# -*- coding: utf-8 -*-
# @Time : 2022/8/11 13:59
# @FileName: TranslateView.py
# @Target:
import os, json, codecs, time
from django.db.models import Q
from django.utils import timezone
import logging

from .utils import idmi_nlp_tool
from .models import TicketModel , CuZuModel # , MediaModel

def _translate_complaints(TicketModel):
    end_date = timezone.now()
    start_date = end_date - timezone.timedelta(hours=24 * 1,
                                               minutes=0, seconds=0)
    _res_list = TicketModel_Class.objects.filter(
        Q(
            Q(FLAG_function_group_problem=True)
        ) &
        Q(create_time__range=(start_date, end_date))
    ).all().values(
        'uuid',
        'brand', 'series',
        'function_group_problem_en'
    )
    for _res in tqdm(_res_list):
        try:
            uuid_str = _res['uuid']
            brand = _res['brand']
            series = _res['series']
            _function_group_problem_en = _res['function_group_problem_en']
            if brand in [
                'SVW', 'FAW-VW', 'VWA', 'Audi', 'Xpeng',
            ]:
                function_group_problem_en = []
                _function_group_problem_en = json.loads(_function_group_problem_en)
                for _d in _function_group_problem_en:
                    _content_list = _d['content_list']
                    content_list = []
                    function_group = _d['function_group']
                    problem = _d['problem']
                    sentiment = _d['sentiment']
                    for _content in _content_list:
                        _content_en = idmi_nlp_tool._translate_util_zh2en(
                            sentence=_content
                        )['en']
                        content_list.append(_content_en)
                        function_group_problem_en.append({
                            'content_list': content_list,
                            'function_group': function_group,
                            'problem': problem,
                            'sentiment': sentiment
                        })
                function_group_problem_en = json.dumps(function_group_problem_en, ensure_ascii=False)
                TicketModel_Class.objects.filter(uuid=uuid_str).update(
                    function_group_problem_en=function_group_problem_en,
                    update_time=timezone.now()
                )
        except:
            ...
    return JsonResponse({'status': 200})


def translate_complaints():
    _translate_complaints(TicketModel=TicketModel)


def _translate_cuzu(CuZuModel):
    end_date = timezone.now()
    start_date = end_date - timezone.timedelta(hours=24 * 1,
                                               minutes=0, seconds=0)
    _res_list = CuZuModel.objects.filter(
        Q(create_time__range=(start_date, end_date))
    ).all().values(
        'uuid',
        'brand', 'series',
        'llm_feeling_en'
    )
    for _res in tqdm(_res_list):
        try:
            uuid_str = _res['uuid']
            brand = _res['brand']
            series = _res['series']
            _llm_feeling_en = _res['llm_feeling_en']
            if brand in [
                'SVW', 'FAW-VW', 'VWA', 'Audi',
            ]:
                llm_feeling_en = []
                _llm_feeling_en = json.loads(_llm_feeling_en)
                for _d in _llm_feeling_en:
                    _content_list = _d['content_list']
                    content_list = []
                    feel_1 = _d['feel_1']
                    feel_2 = _d['feel_2']
                    sentiment = _d['sentiment']

                    for _content in _content_list:
                        _content_en = idmi_nlp_tool._translate_util_zh2en(
                            sentence=_content
                        )['en']
                        content_list.append(_content_en)
                        llm_feeling_en.append({
                            'content_list': content_list,
                            'feel_1': feel_1,
                            'feel_2': feel_2,
                            'sentiment': sentiment
                        })
                llm_feeling_en = json.dumps(llm_feeling_en, ensure_ascii=False)
                CuZuModel.objects.filter(uuid=uuid_str).update(
                    llm_feeling_en=llm_feeling_en,
                    update_time=timezone.now()
                )
                print(uuid_str)
        except:
            ...
    return JsonResponse({'status': 200})


def translate_cuzu():
    _translate_cuzu(CuZuModel=CuZuModel)
